using System;
using UnityEngine;

public class StartGameWindow : MonoBehaviour
{
	public static StartGameWindow instance;

	public GameObject holder;

	public UILabel timerLabelStartRace;

	public UILabel timerLabelFindPlayers;

	private float timeLeft = -1f;

	public bool timerStarted;

	private bool _findPlayers = true;

	public event Action onTimeOut;

	private void Awake()
	{
		instance = this;
		holder.SetActive(false);
	}

	public void StartTimer(float time, bool findPlayers)
	{
		timerStarted = true;
		timeLeft = time;
		_findPlayers = findPlayers;
		if (findPlayers)
		{
			timerLabelFindPlayers.text = ((int)time).ToString();
			timerLabelFindPlayers.gameObject.SetActive(true);
			timerLabelStartRace.gameObject.SetActive(false);
		}
		else
		{
			timerLabelStartRace.text = ((int)time).ToString();
			timerLabelStartRace.gameObject.SetActive(true);
			timerLabelFindPlayers.gameObject.SetActive(false);
		}
		holder.SetActive(true);
	}

	public void StopTimer()
	{
		timeLeft = -1f;
		timerStarted = false;
		timerLabelStartRace.text = string.Empty;
		timerLabelFindPlayers.text = string.Empty;
		holder.SetActive(false);
		if (this.onTimeOut != null)
		{
			this.onTimeOut();
		}
	}

	private void Update()
	{
		if (timerStarted)
		{
			timeLeft -= Time.unscaledDeltaTime;
			if (timeLeft <= 0f || timeLeft > 120f)
			{
				StopTimer();
			}
			else if (_findPlayers)
			{
				timerLabelFindPlayers.text = ((int)timeLeft).ToString();
			}
			else if (timeLeft >= 1f)
			{
				timerLabelStartRace.text = ((int)timeLeft).ToString();
			}
			else
			{
				timerLabelStartRace.text = "GO!";
			}
		}
	}
}
